package com.javaweb.filter;

import java.io.IOException;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import com.javaweb.base.BaseInject;
import com.javaweb.base.po.Interfaces;
import com.javaweb.constant.SystemConstant;
import com.javaweb.constant.SystemConstant.HeadConstant;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
 * 响应头过滤器
 * @author tank2140896
 */
@Component
public class ResponseHeaderFilter extends OncePerRequestFilter {
	
	protected void doFilterInternal(HttpServletRequest request,HttpServletResponse response,FilterChain filterChain) throws ServletException,IOException {
		Environment environment = BaseInject.getEnvironment();
		//x.y.z：x为主版本号；y为次版本号（功能）；z为修订号（bug修复）
		response.addHeader(environment.getProperty("project.version.key"),environment.getProperty("project.version.value"));
		response.addHeader(environment.getProperty("project.info.key"),environment.getProperty("project.info.value"));
		setResponseHeader(request,response,environment);
		filterChain.doFilter(request,response);
	}
	
	private void setResponseHeader(HttpServletRequest request,HttpServletResponse response,Environment environment) {
		Interfaces interfaces = (Interfaces)request.getAttribute(SystemConstant.REQUEST_URL_CHAIN_ATTRIBUTE_NAME);
		if(interfaces!=null) {
			response.addHeader(HeadConstant.RESPONSE_HEADER_URL_PATTERN,interfaces.getUrl());
		}
	}

}
